package cn.controller;

import cn.basic.BaseController;
import cn.dao.RoleDAO;
import cn.factory.DAOFactory;
import cn.model.TRole;
import cn.util.Expression;
import cn.util.Toolsutil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 角色信息数据交互
 * @author WZG
 * @version 2019-10-30
 */
@Controller
@RequestMapping(value="/role")
public class RoleController extends BaseController {


	@RequestMapping(value="/getrolelist")//映射路径,拦截post请求
	public void getRoleList(
			HttpServletRequest request,
			HttpServletResponse response,
			Model model) throws IOException{

	    RoleDAO ardao = DAOFactory.getRoleDAO();
        List list = ardao.getAllRoles();

		if (list!=null) {
			dt.code = dt.FLAG_SUCC;
			dt.msg = "查询成功，共查出" + list.size() + "条记录";
			dt.data = list;
		}else{
			dt.code = dt.FLAG_FAIL;
			dt.msg = "查询失败";
		}
		closeout();
	}

	@RequestMapping(value="/getrolelistByPage")//映射路径,拦截post请求
	public void getAdminRole(
			String rolename,
			Integer page,
			Integer limit) throws IOException{

        RoleDAO ardao = DAOFactory.getRoleDAO();
		Expression exp = new Expression();
		if(rolename != null && !rolename.equals(""))
			exp.andEqu("rolename", rolename, String.class);
		String wherecondition = exp.toString();
		List<TRole> list = ardao.getAllRoleListByPage(wherecondition,page, limit);
		int size = ardao.getAllRolePageAmount(wherecondition);
		if (list!=null) {
			dt.code = dt.FLAG_SUCC;
			dt.msg = "查询成功，共查出" + dt.count;
			dt.data = list;
			dt.count = size;
		}else{
			dt.code = dt.FLAG_FAIL;
			dt.msg = "查询失败";
		}
		closeout();
	}

	@RequestMapping(value="/addRole")
	public void AddRole(
			String Rolename,
			String description,
			Integer deepth,
			Integer parentid,
			HttpServletRequest request,
			HttpServletResponse response) throws IOException{

        RoleDAO ardao = DAOFactory.getRoleDAO();
		TRole newrole = new TRole();
		newrole.setRolename(Rolename);
		newrole.setDescription(description);
		newrole.setCreatetime(Toolsutil.getdatetime());
		newrole.setDeepth(deepth);
		newrole.setParentid(parentid);
		if (ardao.addRole(newrole) > 0) {
			dt.code = dt.FLAG_SUCC;
			dt.msg = "添加成功";
		} else {
			dt.code = dt.FLAG_FAIL;
			dt.msg = "添加失败";
		}
		closeout();
	}

	/*
	 * 实现一个角色的删除
	 * @param userid 用户名
	 * @throws IOException
	 */
	@RequestMapping(value="/deleterole")
	public void deleterole(
			String roleid,
			HttpServletRequest request,
			HttpServletResponse response,
			Model model) throws IOException{

        RoleDAO ardao = DAOFactory.getRoleDAO();
		if (roleid !=null && !roleid.equals("")){
			ardao.deleteRole(Integer.parseInt(roleid));
			dt.code = dt.FLAG_SUCC;
			dt.msg = "删除成功";
		}else {
			dt.code = dt.FLAG_FAIL;
			dt.msg = "删除失败 ";
		}
		closeout();
	}

	@RequestMapping(value="/modifyrole")
	public void modifyrole(
			int Roleid,
			String Rolename,
			String description,
			String createtime,
			Integer deepth,
			Integer parentid,
			HttpServletRequest request,
			HttpServletResponse response,
			Model model) throws IOException{
        RoleDAO ardao = DAOFactory.getRoleDAO();
		TRole role = new TRole();
		role.setRoleid(Roleid);
		role.setRolename(Rolename);
		role.setDescription(description);
		role.setCreatetime(createtime);
		role.setDeepth(deepth);
		role.setParentid(parentid);
		if (ardao.modifyRole(role)) {
			dt.code = dt.FLAG_SUCC;
			dt.msg = "修改成功";
		}else {
			dt.code = dt.FLAG_FAIL;
			dt.msg = "修改失败";
		}
		closeout();
	}
}
